// pages/examonline/examonline.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: {},
    id: "0",//当前题目id
    fid:0,
    length:0,
    index1:"",
    index2:"",
    xz:false,//选择
    yd:false,//阅读
    tp:false,//数学选择
    tk:false,
    jdt:false,
    bx:false,
    axz:true,
    bxz:false,
    answer: [],//当前答案
    state: ["none", "none", "none", "none", "none"],
    state1: ["none", "none", "none", "none"],
    state2: ["none", "none", "none", "none"],
    state3: ["none", "none", "none", "none"],
    state4: ["none", "none", "none", "none"],
    analyze: "none",
    array1: ["A", "B", "C", "D", "E"],
    laststate: "",
    touch: 0,
    touch1: [0, 0, 0, 0]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let apiname = this.apichange(options.id);
    wx.request({
      url: `${app.globalData.apiurl}${apiname}`,
      method: "GET",
      data: {
      },
      success: res => {
        console.log(res)
        let count = res.data[0].length
        let answer = this.data.answer
        for (let i = 0; i < count; i++) {
          answer.push("10");
        }
        wx.setStorageSync("st",res.data);
        this.setData({
          items: res.data[0],
          answer: res.data[0][this.data.id].answer,
          length: count,
          answer:answer
        })
      }
    })
  },
  bindPickerChange: function (res) {
    if (res.target.id == 0) {
      if (res.detail.value == this.data.items[this.data.id].resulta) {
        let ans = this.data.ans;
        ans[0] = "dui";
        this.setData({
          ans: ans,
          analyze1: ""
        })
      }
      this.setData({
        index1: res.detail.value,
        state1: "none",
        img1: "",
      })
    }
    if (res.target.id == 1) {
      if (res.detail.value == this.data.items[this.data.id].resultb) {
        let ans = this.data.ans;
        ans[1] = "dui";
        this.setData({
          ans: ans,
          analyze2: ""
        })
      }
      this.setData({
        index2: res.detail.value,
        state2: "none",
        img2: "",
      })
    }
    console.log(res)
  },
  //下一题点击事件
  bindNext: function (e) {
    let state = this.data.state;
    let touch = [0, 0, 0, 0]
    state = ["none", "none", "none", "none", "none"];
    this.setData({
      state: state,
      state1: state,
      state2: state,
      state3: state,
      state4: state,
      index1: "",
      index2: "",
      analyze: "none",
      laststate: "",
      touch: 0,
      touch1:touch
    })
    if (this.data.id == this.data.length-1) {
      let next = wx.getStorageSync("st");
      if(this.data.fid==next.length-1){
        wx.navigateTo({
          url: '/pages/result/result?answer=' + this.data.answer,
        })
        return false;
      }
      wx.showToast({
        title: `已经做完当前题型！以下题目将不计算分数！可在最后看答案！`,
        icon: "none"
      })
      let fid = this.data.fid+1;
      this.ycthing(fid);
      this.setData({
        fid:fid,
        items:next[fid],
        id:0,
        length:next[fid].length,
        touch:0
      })
      console.log(this.data.items)
    } else {
      let ide = this.data.id * 1 + 1
      console.log(ide)
      this.setData({
        id: ide,
      })
      console.log(this.data.items[this.data.id])
    }
  },
  //上一题点击事件
  bindLast: function (e) {
    let state = this.data.state;
    let touch = [0, 0, 0, 0]
    state = ["none", "none", "none", "none", "none"];
    this.setData({
      state: state,
      state1: state,
      state2: state,
      state3: state,
      state4: state,
      index1:"",
      index2:"",
      analyze: "none",
      touch1:touch,
      touch: 0
    })
    if (this.data.id == 0&& this.data.fid==0) {
      wx.showToast({
        title: '无上一题！',
        icon: "none"
      })
      return false;
    } else {
      if (this.data.id==0){
        let fid = this.data.fid*1-1;
        let tm = wx.getStorageSync("st");
        let length=tm[fid].length;
        this.ycthing(fid);
        this.setData({
          id:length,
          length:length,
          fid:fid,
          items:tm[fid]
        })
      }
      let ide = this.data.id * 1 - 1
      this.setData({
        id: ide,
        touch: 0
      })
      if (ide == 0) {
        this.setData({
          laststate: ""
        })
      }
      console.log(this.data.items[this.data.id])
    }
  },
  //选项点击事件
  chance1: function (e) {
    console.log(e.currentTarget.id) 
      if (e.currentTarget.id >= 0 && e.currentTarget.id <= 3) {
        if (this.data.touch1[0] == 1) {
          return false;
        }
        if (this.data.state1[e.currentTarget.id] == "none") {
          let state = this.data.state1;
          let touch = this.data.touch1;
          touch[0] = 1;
          console.log(state)
          state[e.currentTarget.id] = "";
          console.log(state)
          this.setData({
            state1: state,
            touch1: touch
          })
        }
      }
      if (e.currentTarget.id >= 4 && e.currentTarget.id <= 7) {
        e.currentTarget.id = e.currentTarget.id - 4
        if (this.data.touch1[1] == 1) {
          return false;
        }
        if (this.data.state2[e.currentTarget.id] == "none") {
          let state = this.data.state2;
          let touch = this.data.touch1;
          touch[1] = 1;
          state[e.currentTarget.id] = "";
          this.setData({
            state2: state,
            touch1: touch
          })
        }
      }
      if (e.currentTarget.id >= 8 && e.currentTarget.id <= 11) {
        e.currentTarget.id = e.currentTarget.id - 8
        if (this.data.touch[2] == 1) {
          return false;
        }
        if (this.data.state3[e.currentTarget.id] == "none") {
          let state = this.data.state3;
          let touch = this.data.touch1;
          touch[2] = 1;
          state[e.currentTarget.id] = "";
          this.setData({
            state3: state,
            touch1: touch
          })
        }
      }
      if (e.currentTarget.id >= 12 && e.currentTarget.id <= 15) {
        e.currentTarget.id = e.currentTarget.id - 12
        if (this.data.touch[3] == 1) {
          return false;
        }
        if (this.data.state4[e.currentTarget.id] == "none") {
          let state = this.data.state4;
          let touch = this.data.touch1;
          touch[3] = 1;
          state[e.currentTarget.id] = "";
          this.setData({
            state4: state,
            touch1: touch
          })
        }
      }
      return false;
  },
  //选项
  chance: function (e) {
    if(this.data.fid==2){
      if (this.data.state[e.currentTarget.id] == "none") {
        let state = this.data.state;
        state[e.currentTarget.id] = ""
        this.setData({
          state: state,
        })
      }
    }else{
      if (this.data.touch == 1) {
        return false;
      }
      console.log(this.data.answer[e.currentTarget.id])
      if (this.data.fid == 0) {
        let answer = this.data.answer
        answer[this.data.id] = e.currentTarget.id
        this.setData({
          answer: answer
        })
        console.log(answer)
      }
      if (this.data.state[e.currentTarget.id] == "none") {
        let state = this.data.state;
        state[e.currentTarget.id] = ""
        this.setData({
          state: state,
          analyze: "",
          touch: 1
        })
      }
    }
    
  },
  //隐藏事件
  ycthing:function(fid){
    if(fid==0){
      this.setData({
        bxz:false,
        axz:true,
        tp:false,
        xz:false,
        yd:false,
        dt:false,
        tk: false,
        jdt:false
      })
    }
    if(fid==1){
      this.setData({
        tp: false,
        xz: false,
        yd: false,
        dt: false,
        tk: false,
        jdt: false,
        bx:false,
        axz:false,
        bxz: true
      })
    }
    if (fid == 2) {
      this.setData({
        tp: false,
        xz: false,
        yd: false,
        dt: false,
        tk: false,
        jdt: false,
        bx: false,
        axz: true,
        bxz: false,
      })
    }
  },
  //api名称转换
  apichange: function (id) {
    if(id==7){
      return "zsbYxzh/testPaperListOfZsbYxzh"
    } 
  },
  pretitle: function (e) {
    let id = this.data.id
    console.log(this.data.items[id])
    wx.previewImage({
      urls: [this.data.items[id].title],
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})